package io.github.codelifeleno.jdbrelease.model;


import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.resource.Resource;
import io.github.codelifeleno.jdbrelease.model.finals.JDBReleaseRunStatus;

import java.time.LocalDateTime;
import java.util.StringJoiner;

/**
 * 运行时配置
 */

public class JDBReleaseRunModel {

    /**
     * 运行时ID
     */
    private String runId;
    /**
     * 配置的路径 JDBReleaseConfig.xml 这个路径
     */
    private Resource runConfigFilePath;
    /**
     * 启动时间
     */
    private LocalDateTime runStartTime;
    /**
     * 结束时间
     */
    private LocalDateTime runEndTime;
    /**
     * 运行状态
     */
    private JDBReleaseRunStatus jdbReleaseRunStatus = JDBReleaseRunStatus.READY;
    /**
     * 运行时间间隔
     */
    private TimeInterval runInterval = new TimeInterval();
    /**
     * 时间间隔对象
     */
    private Long interval = 0L;


    public JDBReleaseRunModel(String runId, Resource runConfigFilePath) {
        this.runId = runId;
        this.runConfigFilePath = runConfigFilePath;
    }

    public String getRunId() {
        return runId;
    }

    public void setRunId(String runId) {
        this.runId = runId;
    }

    public Resource getRunConfigFilePath() {
        return runConfigFilePath;
    }

    public void setRunConfigFilePath(Resource runConfigFilePath) {
        this.runConfigFilePath = runConfigFilePath;
    }

    public LocalDateTime getRunStartTime() {
        return runStartTime;
    }

    public void setRunStartTime(LocalDateTime runStartTime) {
        this.runStartTime = runStartTime;
        getRunInterval().start();
    }

    public LocalDateTime getRunEndTime() {
        return runEndTime;
    }

    public void setRunEndTime(LocalDateTime runEndTime) {

        this.runEndTime = runEndTime;
        this.setInterval(this.getRunInterval().interval());
        this.getRunInterval().clear();
    }

    public JDBReleaseRunStatus getRunStatus() {
        return jdbReleaseRunStatus;
    }

    public void setRunStatus(JDBReleaseRunStatus JDBReleaseRunStatus) {
        this.jdbReleaseRunStatus = JDBReleaseRunStatus;
        JDBReleaseRunStatus.changeStatus(this);
    }
    public TimeInterval getRunInterval() {
        return runInterval;
    }

    private void setRunInterval(TimeInterval runInterval) {
        this.runInterval = runInterval;
    }

    public Long getInterval() {
        return interval;
    }

    private void setInterval(Long interval) {
        this.interval = interval;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", JDBReleaseRunModel.class.getSimpleName() + "[", "]")
                .add("runId='" + runId + "'")
                .add("runConfigFilePath='" + runConfigFilePath + "'")
                .add("runStartTime=" + runStartTime)
                .add("runEndTime=" + runEndTime)
                .add("JDBReleaseRunStatus=" + jdbReleaseRunStatus)
                .add("runInterval=" + runInterval)
                .add("interval=" + interval)
                .toString();
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;

        JDBReleaseRunModel that = (JDBReleaseRunModel) o;
        return getRunId().equals(that.getRunId()) && getRunConfigFilePath().equals(that.getRunConfigFilePath());
    }

    @Override
    public int hashCode() {
        int result = getRunId().hashCode();
        result = 31 * result + getRunConfigFilePath().hashCode();
        return result;
    }
}
